<!-- BEGIN PAGE HEADER-->
<style>
    .ibox label {
        margin-left: 8px;
        margin-bottom: 8px;
    }
</style>
<?php $this->load->view('partials/breadcrumb.tpl') ?>
<!-- END PAGE HEADER-->
<!-- BEGIN PAGE CONTENT-->
<script type="text/javascript">
    function defConfirm(url) {
        if (!confirm("确定要删除吗？")) {
            return false;
        }
        window.location.href = url;
        return true;
    }

    $(document).ready(function () {
        $("#reset").click(function () {
            $("select").val('');
            $("input[type='text']").val('');
        });
    });
</script>
<div class="row">
    <div class="col-xs-12">
        <!-- Begin: life time stats -->
        <div class="portlet">
            <?= form_open('', Array('class' => 'form-inline', 'role' => 'form')) ?>
            <?= search_form_input('id', filterValue('id'), '商品ID', 'class="form-control input-xs"'); ?>
            <?= search_form_input('product_name', filterValue('product_name'), '商品名称', 'class="form-control input-xs"'); ?>
            <?= search_form_input('asin', filterValue('asin'), '商品ASIN', 'class="form-control input-xs"'); ?>
            <?= search_form_dropdown('shop_id', array('' => '选择账户...') + $allShop, filterValue('shop_id'), '账户', 'class="form-control input-xs"'); ?>
            <?php if ($currentRoleId != 2 && $currentRoleId != 4): ?>
                <?= search_form_dropdown('group_id', array('' => '选择账户组...') + $allGroups, filterValue('group_id'), '账户组', 'class="form-control input-xs"'); ?>
            <?php endif; ?>
            <?= search_form_dropdown('task_contact_id', array('' => '任务联系人...') + $allDistribution, filterValue('task_contact_id'), '任务联系人', 'class="form-control input-xs"'); ?>
            <?= search_form_dropdown('status', array('' => '选择状态...') + MTask::getState(), filterValue('status'), '状态', 'class="form-control input-xs"'); ?>
            <?= search_form_dropdown('is_urgent', array('' => '是否加急...') + MTask::getUrgent(), filterValue('is_urgent'), '是否加急', 'class="form-control input-xs"'); ?>
            <?= search_form_dropdown('country', array('' => '选择国家...') + IOSS_StaticConfig::getCountryInfo(), filterValue('country'), '国家', 'class="form-control input-xs"'); ?>
            <?php if (in_array($this->_user->role_id, [2, 3, 4, 5])): ?>
                <?= search_form_dropdown('has_accept', array('' => '是否已接...') + [0 => '未接', 1 => '已接'], filterValue('has_accept'), '是否已接', 'class="form-control input-xs"'); ?>
            <?php endif; ?>

            <div style="clear:both;height:14px;"></div>
            <?= search_form_input('task_number_perpage', filterValue('task_number_perpage'), '每页显示条数', 'class="form-control input-xs"'); ?>
            <div class="input-group input-large date-picker input-daterange" data-date="" data-date-format="yyyy-mm-dd">
                <input type="text" class="form-control" name="task_create_time_begin"
                       value="<?= filterValue('task_create_time_begin') ?>" placeholder="开始时间" autoComplete="off">
                <span class="input-group-addon">to </span>
                <input type="text" class="form-control" name="task_create_time_end"
                       value="<?= filterValue('task_create_time_end') ?>" placeholder="结束时间" autoComplete="off">
            </div>

            <button type="button" class="btn default" id="reset">重置条件</button>
            <button type="submit" class="btn green about_search">搜索 <i class="fa fa-search"></i></button>
            <!--            <button type="button" class="btn purple" onclick="iBox()">导出选项</button>-->
<!--            <button type="button" class="btn purple" id="export-xls">原版导出</button>-->
            <button type="button" class="btn red" id="export-xls-python">快速导出</button>
            <button type="button" class="btn yellow" id="ajax_merge">一键接单</button>
            <label style="float: right;margin-right: 20px" class="label label-danger">总计<?php echo $total_number ?>
                条</label>
            <script>
                function iBox() {
                    $('#choose-field').toggle(500, 'swing')
                }
            </script>
            <?= form_close() ?>
            <div style="display:none;" class="portlet-body" id="choose-field">
                <form action="" method="post" id="export_form">
                    <div class="well well-sm" style="margin: 10px 0;">
                        <div class="checkbox" style="margin-bottom: 14px;">
                            <b><i>请选择要导出的字段：</i></b>
                            <label>
                                <input id="all" type="checkbox">
                                全选/全不选
                            </label>
                        </div>
                        <div class="checkbox ibox">
                            <label>
                                <input checked name="field[]" value="id" type="checkbox">id
                            </label>
                            <label>
                                <input checked name="field[]" value="image" type="checkbox">产品图片
                            </label>
                            <label>
                                <input checked name="field[]" value="shop_id" type="checkbox">所属账户
                            </label>
                            <label>
                                <input checked name="field[]" value="related_shop" type="checkbox">下单店铺
                            </label>
                            <label>
                                <input checked name="field[]" value="creator_id" type="checkbox">创建人
                            </label>
                            <label>
                                <input checked name="field[]" value="task_contact_id" type="checkbox">任务联系人
                            </label>
                            <label>
                                <input checked name="field[]" value="group_id" type="checkbox">所属销售组
                            </label>
                            <label>
                                <input checked name="field[]" value="product_name" type="checkbox">产品名称
                            </label>
                            <label>
                                <input checked name="field[]" value="search_key" type="checkbox">搜索关键词
                            </label>
                            <label>
                                <input checked name="field[]" value="amazon_order" type="checkbox">亚马逊排名
                            </label>
                            <label>
                                <input checked name="field[]" value="asin" type="checkbox">asin
                            </label>
                            <label>
                                <input checked name="field[]" value="price" type="checkbox">产品价格
                            </label>
                            <label>
                                <input checked name="field[]" value="country" type="checkbox">国家
                            </label>
                            <label>
                                <input checked name="field[]" value="link" type="checkbox">产品链接
                            </label>
                            <label>
                                <input checked name="field[]" value="sale_need_num" type="checkbox">要求完成销量数
                            </label>
                            <label>
                                <input checked name="field[]" value="sale_finish_num" type="checkbox">已完成销量数
                            </label>
                            <label>
                                <input checked name="field[]" value="comment_need_num" type="checkbox">要求完成测评数
                            </label>
                            <label>
                                <input checked name="field[]" value="comment_finish_num" type="checkbox">已完成测评数
                            </label>

                            <label>
                                <input checked name="field[]" value="apply_comment_num" type="checkbox">我的已接测评数
                            </label>
                            <label>
                                <input checked name="field[]" value="finish_comment_num" type="checkbox">我的完成测评数
                            </label>

                            <label>
                                <input checked name="field[]" value="requirement_desc" type="checkbox">任务说明
                            </label>
                            <label>
                                <input checked name="field[]" value="is_urgent" type="checkbox">是否加急
                            </label>
                            <label>
                                <input checked name="field[]" value="remark" type="checkbox">备注
                            </label>
                            <label>
                                <input checked name="field[]" value="create_time" type="checkbox">创建时间
                            </label>
                            <input type="hidden" name="file_name" value="tasks">
                            <input type="hidden" name="page_name" value="tasks.task.index">
                            <input type="hidden" name="excel_type" value="TaskExport">
                        </div>
                        <div class="text-right">
                            <button id="export-xls" type="button" class="text-right btn blue-madison">导出xls</button>
                        </div>
                        <script>
                            //php导出
                            $('#export-xls').click(function () {
                                var data = $('#export_form').serialize();
                                if (data.indexOf('field') == -1) {
                                    toastr.error('请选择要导出的字段！', '失败');
                                    return false;
                                } else {
                                    $('.loading-x').fadeIn();
                                    $.ajax({
                                        url: '<?=site_url("/common/ExcelExport/excel_export")?>',
                                        data: $('#export_form').serialize(),
                                        method: 'POST',
                                        dataType: 'json',
                                        success: function (output) {
                                            $('.loading-x').fadeOut();
                                            if(output.status){
                                                toastr.success(output.msg, '成功');
                                            }else{
                                                toastr.error(output.msg, '失败');
                                            }
                                            document.location.href = '/' + output.url;
                                        },
                                        fail: function (data) {
                                            toastr.error(data.msg, '失败')
                                        }
                                    })
                                }
                            });

                            //python导出
                            $('#export-xls-python').click(function () {
                                var data = $('#export_form').serialize();
                                if (data.indexOf('field') == -1) {
                                    toastr.error('请选择要导出的字段！', '失败');
                                    return false;
                                } else {
                                    $('.loading-x').fadeIn();
                                    $.ajax({
                                        url: '<?=site_url("/common/ExcelExport/python_export")?>',
                                        data: $('#export_form').serialize(),
                                        method: 'POST',
                                        dataType: 'json',
                                        success: function (output) {
                                            $('.loading-x').fadeOut();
                                            if(output.status){
                                                toastr.success(output.msg, '成功');
                                            }else{
                                                toastr.error(output.msg, '失败');
                                            }
                                            document.location.href = '/' + output.url;
                                        },
                                        fail: function (data) {
                                            toastr.error(data.msg, '失败')
                                        }
                                    })
                                }
                            });

                            $('#all').click(function () {
                                $("input[type='checkbox']").each(function () {
                                    if ($(this).prop("checked") == true) {
                                        $("input[type='checkbox']").prop('checked', true);
                                        $("input[type='checkbox']").parent('span').addClass('checked');
                                        return;
                                    } else {
                                        $("input[type='checkbox']").prop('checked', false);
                                        $("input[type='checkbox']").parent('span').removeClass('checked');
                                        return;
                                    }
                                });
                            });
                        </script>
                    </div>
                </form>
            </div>
            <div class="portlet-body">
                <div class="row">
                    <div class="col-xs-12">
                        <div class="table-scrollable">
                            <form id="ajax_merge_form" action="" method="post" onsubmit="return false">
                                <input type="hidden" name="apply_number" id="apply_number">
                                <table class="table table-striped table-bordered table-hover">
                                    <thead class="flip-content">
                                    <table class="table table-striped table-bordered table-hover">
                                        <thead class="flip-content">
                                        <tr>
                                            <th width="1%">
                                                <label>
                                                    <input type="checkbox" id="merge_all" ">
                                                </label>
                                            </th>
                                            <th width="5%">ID</th>
                                            <th width="5%">所属站点</th>
                                            <th width="5%">下单店铺</th>
                                            <th width="5%">账户组</th>
                                            <th width="5%">利润率</th>
                                            <th width="5%">任务联系人</th>
                                            <th width="5%">商品名称</th>
                                            <th width="5%">搜索关键词</th>
                                            <th width="5%">排名</th>
                                            <th width="5%">商品ASIN</th>
                                            <th width="5%">国家</th>
                                            <th width="5%">商品链接</th>
                                            <th width="5%">商品图片</th>
                                            <th width="3%">已接/销量需求</th>
                                            <th width="3%">已接/测评需求</th>
                                            <th width="2%">加急</th>
                                            <th width="5%">完成进度(已完成/已接)</th>
                                            <th width="6%">创建时间</th>
                                            <th>状态</th>
                                            <th width="20%">操作</th>
                                        </tr>
                                        </thead>
                                        <tbody>
                                        <?php foreach ($list as $v) { ?>
                                            <tr>
                                                <td>
                                                    <input type="checkbox" class="mbox" name="task_ids[]" value="<?= $v->id ?>" title="ID:<?= $v->id ?>">
                                                </td>
                                                <td class="numeric"><?= $v->id ?></td>
                                                <td class="numeric"><?= htmlspecialchars($allShop[$v->shop_id]) ?></td>
                                                <td class="numeric"><?= $v->related_shop ?></td>
                                                <td class="numeric"><?= htmlspecialchars($allGroups[$v->group_id]) ?></td>
                                                <td class="numeric"><?= $v->profit_rate ?>%</td>
                                                <td class="numeric"><?= htmlspecialchars($allDistribution[$v->task_contact_id]) ?></td>
                                                <td class="numeric" title="<?= htmlspecialchars($v->product_name) ?>"><?= mb_substr(htmlspecialchars($v->product_name),0,8) ?></td>
                                                <td class="numeric"><?= htmlspecialchars($v->search_key) ?></td>
                                                <td class="numeric"><?= htmlspecialchars($v->amazon_order) ?></td>
                                                <td class="numeric"><?= htmlspecialchars($v->asin) ?></td>
                                                <td class="numeric"><?= IOSS_StaticConfig::getCountryInfo($v->country) ?>
                                                </td>
                                                <td class="numeric"><a class="btn blue-dark"
                                                                       href="<?= htmlspecialchars($v->link) ?>"
                                                                       target="_blank">查看商品</a>
                                                </td>
                                                <td class="numeric">

                                                    <img class="img-rounded" src="<?= image_url($v->image) ?>" width="80"
                                                         height="auto"/>
                                                </td>
                                                <td class="numeric text-center">
                                                    <button data-operator_id="<?= $this->_user->id ?>"
                                                            data-task_id="<?= $v->id ?>"
                                                            data-task_left_num="<?= $v->sale_need_num - $v->sale_applied_num ?>"
                                                            data-status="<?= $v->sale_applied_num ? $v->sale_applied_num / $v->sale_need_num : $v->sale_applied_num ?>"
                                                            data-type="sale"
                                                            class="btn blue apply_btn"><?= $v->sale_applied_num . '/' . $v->sale_need_num; ?></button>
                                                </td>
                                                <td class="numeric text-center">
                                                    <button data-operator_id="<?= $this->_user->id ?>"
                                                            data-task_id="<?= $v->id ?>"
                                                            data-status="<?= $v->comment_applied_num ? $v->comment_applied_num / $v->comment_need_num : $v->comment_applied_num; ?>"
                                                            data-task_left_num="<?= $v->comment_need_num - $v->comment_applied_num ?>" data-type="comment"
                                                            class="btn purple apply_btn"><?= $v->comment_applied_num . '/' . $v->comment_need_num; ?></button>
                                                </td>

                                                <td class="numeric"><?= MTask::getUrgent($v->is_urgent); ?></td>
                                                <td class="numeric text-center">
                                                    <div>
                                                        <p>
                                                            <?= $v->apply_sale_num ? '<label style="font-size: 14px" class="label label-primary">' . $v->finish_sale_num . '/' . $v->apply_sale_num . '</label>' . '     <label class="label ' . ((int)($v->finish_sale_num / $v->apply_sale_num * 100) > 80 ? 'label-success' : 'label-danger') . '" style="font-size: 14px;">销量(' . (int)($v->finish_sale_num / $v->apply_sale_num * 100) . '%)</label>' : '<label class="label label-warning" style="font-size: 14px">暂未接销量单</label>' ?>
                                                        </p>
                                                        <p>
                                                            <?= $v->apply_comment_num ? '<label style="font-size: 14px" class="label label-primary">' . $v->finish_comment_num . '/' . $v->apply_comment_num . '</label>' . '     <label class="label ' . ((int)($v->finish_comment_num / $v->apply_comment_num * 100) > 80 ? 'label-success' : 'label-danger') . '" style="font-size: 14px">测评(' . (int)($v->finish_comment_num / $v->apply_comment_num * 100) . '%)</label>' : '<label class="label label-warning" style="font-size: 14px;">暂未接测评单</label>' ?>
                                                        </p>
                                                    </div>
                                                </td>
                                                <td class="numeric"><?= date('Y-m-d H:i', $v->create_time) ?></td>
                                                <td><?= $v->status?'<span class="label label-success">可用</span>':'<span class="label label-danger">禁用</span>' ?></td>
                                                <td class="numeric text-center">
                                                    <div class="btn-group btn-group-solid">
                                                        <a href="<?= site_url($thisModule . $thisController . '/view/' . $v->id) ?>"
                                                           class="btn btn-sm blue <?= $p->read ? '' : 'disabled' ?>"><i
                                                                    class="fa fa-play"></i> 查看</a>
                                                        <a href="<?= site_url($thisModule . $thisController . '/edit/' . $v->id) ?>"
                                                           class="btn btn-sm purple <?= ($p->edit && $this->_user->id == $v->creator_id) || $this->_user->role_id == 2 ? '' : 'disabled' ?>"><i
                                                                    class="fa fa-edit"></i> 编辑</a>
                                                        <a href="javascript:ioss_url_modal('<?= site_url('/tasks/task/ajaxViewRecordNum/' . $v->id) ?>');"
                                                           class="btn btn-sm yellow-gold">
                                                            <i class="fa fa-list"></i> 接单情况</a>
                                                        <?php if ($p->delete) { ?>
                                                            <a style="display: none;" class="black"
                                                               href="javascript:void(defConfirm('<?= site_url($thisModule . $thisController . '/delete/' . $v->id . '/' . $v->update_time) ?>'));"><i
                                                                        class="fa fa-trash-o"></i> <?= l('delete') ?></a>
                                                        <?php } ?>
                                                    </div>
                                                </td>
                                            </tr>
                                        <?php } ?>
                                        </tbody>
                                    </table>
                            </form>
                        </div>
                    </div>
                </div>
                <div class="row">
                    <div class="col-xs-12 text-right">
                        <?= $pagination ?>
                    </div>
                </div>
            </div>
        </div>
        <!-- End: life time stats -->
    </div>
</div>

<!-- loading层 -->
<style>
    .loading-x{
        display: none;
        position: fixed;
        top: 0;
        left: 0;
        right: 0;
        bottom: 0;
        background: #0000008a;
        z-index: 20050;
    }
    .x-contain{
        margin: 18%;
        text-align: center;
    }
</style>
<div class="loading-x">
    <div class="x-contain">
        <img src="/static/assets/20160618173856_Ys5Td.gif" alt="">
    </div>
</div>

<div class="modal fade apply-modal">
    <div class="modal-dialog" role="document">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span>
                </button>
                <h4 class="modal-title apply-title">接单</h4>
            </div>
            <div class="modal-body">
                <form id="apply_form">
                    <div class="form-group">
                        <label class="control-label">该任务剩余可接单数量:</label>
                        <label id="left-apply-num" class="label label-danger"
                               style="font-size: 14px;margin-left: 12px;">加载中...</label>
                    </div>

                    <div class="form-group">
                        <label for="apply_num" class="control-label">接单数量:</label>
                        <input type="number" required name="apply_num" class="form-control" id="apply_num">
                        <input type="hidden" name="operator_id" class="form-control">
                        <input type="hidden" name="task_id" class="form-control">
                        <input type="hidden" name="apply_type" class="form-control">
                    </div>
                </form>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-default" data-dismiss="modal">取消</button>
                <button type="button" class="btn btn-primary apply-submit">确定</button>
            </div>
        </div>
    </div>
</div>
<div class="modal fade group-apply-modal">
    <div class="modal-dialog" role="document">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span>
                </button>
                <h4 class="modal-title apply-title">接单</h4>
            </div>
            <div class="modal-body">
                <form id="group-apply-form">
                    <div class="form-group">
                        <label for="apply_num" class="control-label">接单数量:</label>
                        <input type="number" required name="group_apply_num" class="form-control" id="group-apply-num" placeholder="请输入接取数量">
                        <input type="hidden" name="operator_id" class="form-control">
                        <input type="hidden" name="task_id" class="form-control">
                        <input type="hidden" name="apply_type" class="form-control">
                    </div>
                </form>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-default" data-dismiss="modal">取消</button>
                <button type="button" class="btn btn-primary group-apply-submit">确定</button>
            </div>
        </div>
    </div>
</div>
<script type="text/javascript">
    $(document).ready(function () {
        $('.img-rounded').zoomify();
        $('#merge_all').click(function () {
            if ($(this).prop("checked") == true) {
                $(".mbox").prop('checked', true);
                $(".mbox").parent('span').addClass('checked');
                return;
            } else {
                $(".mbox").prop('checked', false);
                $(".mbox").parent('span').removeClass('checked');
                return;
            }
        });

    });

    $('#ajax_merge').click(function () {
        var data = $('#ajax_merge_form').serialize();
        if (data.indexOf('task_ids') == -1) {
            toastr.error('请选择要接取的任务！', '失败');
            return;
        }
        $('.group-apply-modal').modal();
    });

    $(".group-apply-submit").click(function () {
        var num = $("#group-apply-num").val();
        if (!num){
            toastr.error('请输入要接取的数量！', '失败');
            return;
        }
        $("#apply_number").val(num);
        var data = $('#ajax_merge_form').serialize();
        $.ajax({
            url: '<?=site_url("/tasks/taskrecorditem/group_tasks")?>',
            data: data,
            method: 'POST',
            dataType: 'json',
            success: function (data) {
                if (data.status) {
                    toastr.success(data.msg, '成功');
                    setTimeout(function () {
                        location.href = '<?=site_url("/tasks/task/index");?>';
                    }, 1000)
                } else {
                    toastr.error(data.msg, '失败')
                }
            },
            fail: function (e) {
                toastr.error(data.msg, '失败')
            }
        })
    })

    $('.apply_btn').click(function () {
        if (<?= in_array($this->_user->role_id, [2, 3, 4, 5]) ? 1 : 0 ?>) {
            var status = this.dataset.status;
            if (status == 1) {
                toastr.error('该任务当前类型可接数目已满！', '失败')
            } else {
                $('#left-apply-num').html(parseInt(this.dataset.task_left_num));
                $('.apply-modal').modal();
                $("input[name='operator_id']").val(this.dataset.operator_id);
                $("input[name='task_id']").val(this.dataset.task_id);
                $("input[name='apply_type']").val(this.dataset.type);
            }
        }
    });
    $('.apply-submit').click(function () {
        var that = $(this);
        if ($("input[name='apply_num']").val() == '' || parseInt($("input[name='apply_num']").val()) > parseInt($('#left-apply-num').html())) {
            console.log($("input[name='apply_num']").val());
            console.log($('#left-apply-num').html());
            toastr.error('接单数量错误(不可为空且不可大于最大可接单数)！', '失败');
            return;
        }
        that.attr('disabled', 'disabled');
        $.ajax({
            url: "<?=site_url('tasks/task/ajaxTaskApply')?>",
            method: 'POST',
            data: $('#apply_form').serializeArray(),
            dataType: 'json',
            success: function (res) {
                if (res.success) {
                    toastr.success(res.msg, '成功');
                    setTimeout(function () {
                        that.removeAttr('disabled');
                        location.reload();
                    }, 1000)
                } else {
                    toastr.error(res.msg, '失败');
                    that.removeAttr('disabled');
                }
            },
            fail: function (res) {
                console.log(res);
            }
        });
    });
</script>
